package com.jumi.microservice.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jumi.microservice.constant.ExceptionEnum;
import com.jumi.microservice.domain.ImageSpaceDO;
import com.jumi.microservice.domain.ImageSpaceDTO;
import com.jumi.microservice.mapper.ImageSpaceMapper;
import com.jumi.microservice.service.ImageSpaceService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 商城相册图片表 服务实现类
 * </p>
 *
 * @author liuz
 * @since 2020-07-27
 */
@Service
@Transactional
public class ImageSpaceServiceImpl extends ServiceImpl<ImageSpaceMapper, ImageSpaceDO> implements ImageSpaceService {

    /**
     * 添加照片
     * @param dtos
     */
    @Override
    public void insert(List<ImageSpaceDTO> dtos) {
        LocalDateTime now = LocalDateTime.now();
        dtos.stream().forEach(dto -> {
            ImageSpaceDO po = dto2Po(dto);
            po.setCreateTime(now);
            po.setUpdateTime(now);
            baseMapper.insert(po);
        });
    }

    /**
     * 删除图片
     * @param ids 主键id
     */
    @Override
    public void delete(String ids) {
        ExceptionEnum.FAIL_ID_NULL.doThrowIf(StringUtils.isEmpty(ids));
        //图片真删
        baseMapper.delete(
                Wrappers.<ImageSpaceDO>lambdaQuery()
                        .in(ImageSpaceDO::getId,ids.split(",")));
    }

    /**
     * 修改图片名
     * @param dto
     */
    @Override
    public void updateIg(ImageSpaceDTO dto) {
        ExceptionEnum.FAIL_IMAGE_NULL.doThrowIf(dto.getId()==null);
        ImageSpaceDO po = baseMapper.selectById(dto.getId());
        ExceptionEnum.FAIL_IMAGE_NULL.doThrowIf(po==null);
        po.setUpdateTime(LocalDateTime.now());
        po.setModifier(dto.getModifier());
        po.setImageName(dto.getImageName());
        baseMapper.updateById(po);
    }

    /**
     * 转移相册
     * @param photoAlbumId 相册id
     * @param ids 图片ids
     * @param username 操作用户
     */
    @Override
    public void change(Long photoAlbumId, String ids, String username) {
        ExceptionEnum.FAIL_ALBLUM_NULL.doThrowIf(photoAlbumId==null);
        ExceptionEnum.FAIL_IMAGE_NULL.doThrowIf(StringUtils.isEmpty(ids));
        String[] idss = ids.split(",");
        Arrays.stream(idss).forEach(id->{
            ImageSpaceDO po = baseMapper.selectById(id);
            if(po!=null){
                po.setPhotoAlbumId(photoAlbumId);
                po.setModifier(username);
                po.setUpdateTime(LocalDateTime.now());
                baseMapper.updateById(po);
            }
        });
    }

    /**
     * dto->po
     * @param dto
     * @return
     */
    private ImageSpaceDO dto2Po(ImageSpaceDTO dto){
        ImageSpaceDO po = null;
        try {
            po = dto.clone(ImageSpaceDO.class);
        }catch (Exception e){
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return po;
    }

    /**
     * po->dto
     * @param po
     * @return
     */
    private ImageSpaceDTO po2Dto(ImageSpaceDO po){
        ImageSpaceDTO dto = null;
        try {
            dto = po.clone(ImageSpaceDTO.class);
        }catch (Exception e){
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return dto;
    }
}
